//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me;

import com.incors.plaf.alloy.AlloyLookAndFeel;
import com.incors.plaf.alloy.DefaultAlloyTheme;
import com.incors.plaf.alloy.themes.acid.AcidTheme;
import com.incors.plaf.alloy.themes.bedouin.BedouinTheme;
import com.incors.plaf.alloy.themes.glass.GlassTheme;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.plaf.metal.MetalLookAndFeel;
import org.jpgolay.me.agency.AgencyController;
import org.jpgolay.me.build.BuildController;
import org.jpgolay.me.buy.OLD_BuyListGUI;
import org.jpgolay.me.customer.CustomerController;
import org.jpgolay.me.customer.OLD_CustomerListGUI;
import org.jpgolay.me.db.Db;
import org.jpgolay.me.db.DbController;
import org.jpgolay.me.export.ExportController;
import org.jpgolay.me.family.FamilyController;
import org.jpgolay.me.item.ItemController;
import org.jpgolay.me.item.OLD_ItemListGUI;
import org.jpgolay.me.msg.MsgController;
import org.jpgolay.me.persistence.ConnectDB;
import org.jpgolay.me.persistence.ObjectCRUD;
import org.jpgolay.me.persistence.ObjectServiceCRUD;
import org.jpgolay.me.persistence.UtilityDB;
import org.jpgolay.me.piece.PiecesController;
import org.jpgolay.me.preferences.PreferencesCRUD;
import org.jpgolay.me.preferences.PreferencesController;
import org.jpgolay.me.preferences.PreferencesGUI;
import org.jpgolay.me.print.options.PrintOptionsController;
import org.jpgolay.me.propertie.PropertyCRUD;
import org.jpgolay.me.propertie.PropertyGUI;
import org.jpgolay.me.sale.OLD_SaleListGUI;
import org.jpgolay.me.salesperson.SalesPersonController;
import org.jpgolay.me.search.SearchController;
import org.jpgolay.me.statistic.StatisticsController;
import org.jpgolay.me.stock.StockController;
import org.jpgolay.me.supplier.OLD_SupplierListGUI;
import org.jpgolay.me.supplier.SupplierController;
import org.jpgolay.me.tax.TaxController;
import org.jpgolay.me.user.ConnectionGUI;
import org.jpgolay.me.user.User;
import org.jpgolay.me.user.UserController;
import org.jpgolay.me.util.BrowserControl;
import org.jpgolay.me.util.MEDate;
import org.jpgolay.me.util.Parameter;

// Referenced classes of package org.jpgolay.me:
//            Messages, AppMainGUI, AppMenuGUI, AppToolBarGUI, 
//            AboutGUI
public class MainController {

    public MainController() {
        msg = false;
        prop = new PropertyCRUD();
        AlloyLookAndFeel.setProperty("alloy.licenseCode", "e#Technics_Services#dvjoxh#c54m1g");
        AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
        iconIF = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/jgIcon.gif"));
        init();
    }

    private void init() {
        connectDB = new ConnectDB();
        if (connectDB.isConnected()) {
            crud = new ObjectCRUD();
            connectionGUI();
        } else {
            JOptionPane.showMessageDialog(null, Messages.getString("MainController.not_db_connection"));
            int reponse = JOptionPane.showConfirmDialog(null, Messages.getString("MainController.do_you_want_configure") + Messages.getString("MainController.admin_fonctionality"), Messages.getString("MainController.confirmation"), 1, 2, iconIF);
            switch (reponse) {
                case 0: // '\0'
                    dbAccess();
                    break;

                case 1: // '\001'
                    JOptionPane.showMessageDialog(null, Messages.getString("MainController.contact_admin"));
                    close();
                    break;

                case 2: // '\002'
                    JOptionPane.showMessageDialog(null, Messages.getString("MainController.contact_admin"));
                    close();
                    break;

                case -1:
                    JOptionPane.showMessageDialog(null, Messages.getString("MainController.contact_admin"));
                    close();
                    break;
            }
        }
    }

    private void dbAccess() {
        propGui = new PropertyGUI();
        try {
            AlloyLookAndFeel.setProperty("alloy.licenseCode", "e#Technics_Services#dvjoxh#c54m1g");
            AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
            com.incors.plaf.alloy.AlloyTheme theme = new GlassTheme();
            javax.swing.LookAndFeel lnf = new AlloyLookAndFeel(theme);
            UIManager.setLookAndFeel(lnf);
            SwingUtilities.updateComponentTreeUI(propGui);
        } catch (Exception e) {
            e.printStackTrace();
        }
        propGui.setDefaultCloseOperation(0);
        propGui.getCancelB().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                close();
            }
        });
        propGui.getOkB().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                propertyUpdate();
                init();
            }
        });
        propGui.setAdminPass(prop.getFirstPass());
        propGui.setAdminUrl(prop.getFirstUrl());
        propGui.setAdminUser(prop.getFirstUser());
        propGui.setDbBackup(prop.getBackupHome());
        propGui.setDbDriver(prop.getDriver());
        propGui.setDbHome(prop.getDataBaseHome());
        propGui.setIp(prop.getIP());
        propGui.setSerial(Integer.toString(prop.getSerial()));
        propGui.setWiPass(prop.getPass());
        propGui.setWiUrl(prop.getUrl());
        propGui.setWiUser(prop.getUser());
        screen = Toolkit.getDefaultToolkit().getScreenSize();
        propGui.setBounds((screen.width - 466) / 2, (screen.height - 340) / 2, 466, 340);
        propGui.setVisible(true);
    }

    private void propertyUpdate() {
        UtilityDB uDB = null;
        prop.setFirstPass(propGui.getAdminPass());
        prop.setFirstUrl(propGui.getAdminUrl());
        prop.setFirstUser(propGui.getAdminUser());
        prop.setBackupHome(propGui.getDbBackup());
        prop.setDriver(propGui.getDbDriver());
        prop.setDataBaseHome(propGui.getDbHome());
        prop.setIp(propGui.getIp());
        try {
            prop.setSerial(Integer.parseInt(propGui.getSerial()));
        } catch (NumberFormatException e) {
            prop.setSerial(0);
        }
        prop.setPass(propGui.getWiPass());
        prop.setUrl(propGui.getWiUrl());
        prop.setUser(propGui.getWiUser());
        prop.update();
        if (propGui.isDbRemove()) {
            uDB = new UtilityDB();
            uDB.deleteDefaultMysqlDB();
        }
        if (propGui.isDbNew()) {
            if (uDB == null) {
                uDB = new UtilityDB();
            }
            uDB.createDefaultMysqlDB(null);
        }
        propGui.setVisible(false);
        propGui = null;
        prop = null;
    }

    private void connectionGUI() {
        PreferencesCRUD pref = new PreferencesCRUD();
        changeLookAndFeel(Integer.parseInt(pref.getLookAndFeel()), false);
        connection = new ConnectionGUI();
        connection.setDefaultCloseOperation(0);
        connection.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                exit();
            }
        });
        JButton v = connection.getValiderB();
        JButton a = connection.getAnnulerB();
        JTextField p = connection.getPasswordPF();
        v.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                connection();
            }
        });
        p.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                connection();
            }
        });
        a.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                exit();
            }
        });
        screen = Toolkit.getDefaultToolkit().getScreenSize();
        connection.setBounds((screen.width - 350) / 2, (screen.height - 120) / 2, 350, 120);
        connection.setVisible(true);
    }

    private void connection() {
        String pass = "";
        char passtab[] = connection.getPassword();
        for (int i = 0; i < passtab.length; i++) {
            pass = pass + passtab[i];
        }

        User usr = (User) crud.read(connection.getUtilisateur(), 19);
        if (usr != null) {
            if (usr.getPassword().equals(pass)) {
                if (!usr.getExpiryDate().equals("")) {
                    if ((new MEDate()).todayCompareTo(usr.getExpiryDate()) <= 0) {
                        param = new Parameter();
                        param.setAccessLaw(Long.toBinaryString(usr.getLaw().intValue()));
                        PreferencesCRUD pref = new PreferencesCRUD();
                        param.setMinimumDecimalNumber(new Integer(pref.getNumberOfDecimal()));
                        param.setRoundType(new Integer(pref.getRoundType()));
                        param.setMinimumProfitMargin(pref.getMinimumProfitMargin());
                        param.setPrintCustomerAssetLaw(pref.getPrintCustomerAssetLaw());
                        param.setPrintCustomerBillLaw(pref.getPrintCustomerBillLaw());
                        param.setPrintCustomerDeliveryLaw(pref.getPrintCustomerDeliveryLaw());
                        param.setPrintCustomerOrderLaw(pref.getPrintCustomerOrderLaw());
                        param.setPrintCustomerQuoteLaw(pref.getPrintCustomerQuoteLaw());
                        param.setPrintSupplierAssetLaw(pref.getPrintSupplierAssetLaw());
                        param.setPrintSupplierBillLaw(pref.getPrintSupplierBillLaw());
                        param.setPrintSupplierOrderLaw(pref.getPrintSupplierOrderLaw());
                        if (usr.getMsg() != null && usr.getMsg().length() > 2) {
                            msgTxt = usr.getMsg();
                            msg = true;
                        }
                        usr.setNumConsult(new Integer(usr.getNumConsult().intValue() + 1));
                        crud.update(usr, 19);
                        param.setUser(usr);
                        connection.setVisible(false);
                        connection = null;
                        initApp();
                    } else {
                        connection.initFields();
                        JOptionPane.showMessageDialog(null, "Votre acc\350s \340 JGestCom a expir\351 !\nPrenez contact avec votre responsable.");
                    }
                } else {
                    param = new Parameter();
                    param.setAccessLaw(Long.toBinaryString(usr.getLaw().intValue()));
                    PreferencesCRUD pref = new PreferencesCRUD();
                    param.setMinimumDecimalNumber(new Integer(pref.getNumberOfDecimal()));
                    param.setRoundType(new Integer(pref.getRoundType()));
                    param.setMinimumProfitMargin(pref.getMinimumProfitMargin());
                    param.setPrintCustomerAssetLaw(pref.getPrintCustomerAssetLaw());
                    param.setPrintCustomerBillLaw(pref.getPrintCustomerBillLaw());
                    param.setPrintCustomerDeliveryLaw(pref.getPrintCustomerDeliveryLaw());
                    param.setPrintCustomerOrderLaw(pref.getPrintCustomerOrderLaw());
                    param.setPrintCustomerQuoteLaw(pref.getPrintCustomerQuoteLaw());
                    param.setPrintSupplierAssetLaw(pref.getPrintSupplierAssetLaw());
                    param.setPrintSupplierBillLaw(pref.getPrintSupplierBillLaw());
                    param.setPrintSupplierOrderLaw(pref.getPrintSupplierOrderLaw());
                    if (usr.getMsg() != null && usr.getMsg().length() > 2) {
                        msgTxt = usr.getMsg();
                        msg = true;
                    }
                    usr.setNumConsult(new Integer(usr.getNumConsult().intValue() + 1));
                    crud.update(usr, 19);
                    param.setUser(usr);
                    connection.setVisible(false);
                    connection = null;
                    initApp();
                }
            } else {
                connection.initFields();
                JOptionPane.showMessageDialog(null, Messages.getString("MainController.connection_msg1") + Messages.getString("MainController.connection_msg2"));
            }
        } else {
            connection.initFields();
            JOptionPane.showMessageDialog(null, Messages.getString("MainController.connection_msg1") + Messages.getString("MainController.connection_msg2"));
        }
    }

    private void initApp() {
        if (appGui == null) {
            screen = Toolkit.getDefaultToolkit().getScreenSize();
            appGui = new AppMainGUI();
            appGui.setDefaultCloseOperation(0);
            PreferencesCRUD pref = new PreferencesCRUD();
            appGui.getMenu().setLookAndFell(Integer.parseInt(pref.getLookAndFeel()));
            setLaw();
            initListenerMM();
            appGui.setBounds(0, 0, screen.width, screen.height - 30);
            appGui.setVisible(true);
            param.setDesktop(appGui.getDesktop());
            if (msg) {
                msgGUI();
            }
            appGui.setTitle(appGui.getTitle() + " " + companyName());
        } else {
            JOptionPane.showMessageDialog(null, Messages.getString("MainController.active_session"));
        }
    }

    private String companyName() {
        ObjectServiceCRUD oscrud = new ObjectServiceCRUD();
        Db db = (Db) oscrud.read(new Integer(prop.getSerial()), 51);
        if (db != null) {
            return "(" + db.getName() + ")";
        } else {
            return "";
        }
    }

    private void setLaw() {
        String b = param.getAccessLaw();
        int position = b.length();
        if (--position >= 0 && b.charAt(position) == '1') {
            appGui.getMenu().getPieces().setEnabled(true);
            appGui.getToolBar().getPieces().setEnabled(true);
            appGui.getMenu().getBuyList().setEnabled(true);
            appGui.getMenu().getSalesList().setEnabled(true);
        }
        position--;
        if (--position >= 0 && b.charAt(position) == '1') {
            appGui.getMenu().getStock().setEnabled(true);
            appGui.getToolBar().getStock().setEnabled(true);
        }
        position--;
        if (--position >= 0 && b.charAt(position) == '1') {
            appGui.getMenu().getSearch().setEnabled(true);
            appGui.getToolBar().getSearch().setEnabled(true);
        }
        position--;
        if (--position >= 0 && b.charAt(position) == '1') {
            appGui.getMenu().getSalespersonManager().setEnabled(true);
            appGui.getToolBar().getSalespersonManager().setEnabled(true);
        }
        position--;
        if (--position >= 0 && b.charAt(position) == '1') {
            appGui.getMenu().getStatistics().setEnabled(true);
            appGui.getToolBar().getStatistics().setEnabled(true);
        }
        position--;
        if (--position >= 0 && b.charAt(position) == '1') {
            appGui.getMenu().getMaking().setEnabled(true);
            appGui.getToolBar().getMaking().setEnabled(true);
        }
        position--;
        if (--position >= 0 && b.charAt(position) == '1') {
            appGui.getMenu().getItem().setEnabled(true);
            appGui.getMenu().getItemList().setEnabled(true);
        }
        position--;
        if (--position >= 0 && b.charAt(position) == '1') {
            appGui.getMenu().getCustomer().setEnabled(true);
            appGui.getMenu().getCustomerList().setEnabled(true);
        }
        position--;
        if (--position >= 0 && b.charAt(position) == '1') {
            appGui.getMenu().getSupplier().setEnabled(true);
            appGui.getMenu().getSupplierList().setEnabled(true);
        }
        position--;
        if (--position >= 0 && b.charAt(position) == '1') {
            appGui.getMenu().getFamily().setEnabled(true);
        }
        position--;
        if (--position >= 0 && b.charAt(position) == '1') {
            appGui.getMenu().getTaxs().setEnabled(true);
            appGui.getMenu().getAgency().setEnabled(true);
        }
        position--;
        if (--position >= 0 && b.charAt(position) == '1') {
            appGui.getMenu().getExport().setEnabled(true);
        }
        position--;
        if (--position >= 0 && b.charAt(position) == '1') {
            appGui.getMenu().getReporting().setEnabled(true);
        }
        position--;
        if (--position >= 0 && b.charAt(position) == '1') {
            appGui.getMenu().getPreferences().setEnabled(true);
        }
        position--;
        if (--position >= 0 && b.charAt(position) == '1') {
            appGui.getMenu().getUsers().setEnabled(true);
            appGui.getMenu().getOpen().setEnabled(true);
        }
    }

    private void initListenerMM() {
        appGui.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                exit();
            }
        });
        appGui.getMenu().getExit().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                exit();
            }
        });
        appGui.getMenu().addPropertyChangeListener("look", new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent e) {
                changeLookAndFeel(appGui.getMenu().getSelectedLookAndFell(), true);
            }
        });
        appGui.getMenu().getAbout().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                aboutGUI();
            }
        });
        appGui.getMenu().getHelp().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                help();
            }
        });
        appGui.getMenu().getReporting().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                printingModel();
            }
        });
        appGui.getMenu().getPieces().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                piecesGUI();
            }
        });
        appGui.getToolBar().getPieces().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                piecesGUI();
            }
        });
        appGui.getMenu().getStock().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                stockGUI();
            }
        });
        appGui.getToolBar().getStock().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                stockGUI();
            }
        });
        appGui.getMenu().getAgency().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                agencyGUI();
            }
        });
        appGui.getMenu().getItem().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                itemGUI();
            }
        });
        appGui.getMenu().getCustomer().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                customerGUI();
            }
        });
        appGui.getMenu().getSalespersonManager().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                salesPersonGUI();
            }
        });
        appGui.getToolBar().getSalespersonManager().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                salesPersonGUI();
            }
        });
        appGui.getMenu().getFamily().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                familyGUI();
            }
        });
        appGui.getMenu().getSupplier().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                supplierGUI();
            }
        });
        appGui.getMenu().getTaxs().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                taxGUI();
            }
        });
        appGui.getMenu().getUsers().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                userGUI();
            }
        });
        appGui.getMenu().getOpen().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                dbGUI();
            }
        });
        appGui.getMenu().getBuyList().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                buyListGUI();
            }
        });
        appGui.getMenu().getItemList().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                itemListGUI();
            }
        });
        appGui.getMenu().getCustomerList().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                customerListGUI();
            }
        });
        appGui.getMenu().getSupplierList().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                supplierListGUI();
            }
        });
        appGui.getMenu().getSalesList().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                saleListGUI();
            }
        });
        appGui.getMenu().getExport().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                exportGUI();
            }
        });
        appGui.getMenu().getMaking().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                JOptionPane.showMessageDialog(null, "Ce module n'est pas install\351 !");
            }
        });
        appGui.getToolBar().getMaking().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                JOptionPane.showMessageDialog(null, "Ce module n'est pas install\351 !");
            }
        });
        appGui.getMenu().getStatistics().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                JOptionPane.showMessageDialog(null, "Ce module n'est pas install\351 !");
            }
        });
        appGui.getToolBar().getStatistics().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                JOptionPane.showMessageDialog(null, "Ce module n'est pas install\351 !");
            }
        });
        appGui.getMenu().getPreferences().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                preferencesGUI();
            }
        });
        appGui.getMenu().getSearch().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                JOptionPane.showMessageDialog(null, "Ce module n'est pas install\351 !");
            }
        });
        appGui.getToolBar().getSearch().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                JOptionPane.showMessageDialog(null, "Ce module n'est pas install\351 !");
            }
        });
    }

    private void msgGUI() {
        if (msgCtrl == null || msgCtrl.isClosed()) {
            msgCtrl = null;
            msgCtrl = new MsgController(msgTxt);
            msgCtrl.setIconifiable(false);
            msgCtrl.setResizable(false);
            msgCtrl.setMaximizable(false);
            msgCtrl.setClosable(true);
            screen = appGui.getDesktop().getSize();
            msgCtrl.setBounds(screen.width / 2 - 150, screen.height / 2 - 100, 300, 200);
            appGui.getDesktop().add(msgCtrl, JLayeredPane.DEFAULT_LAYER);
        }
        if (msgCtrl.isIcon()) {
            try {
                msgCtrl.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!msgCtrl.isVisible()) {
            msgCtrl.setVisible(true);
        }
        msgCtrl.toFront();
    }

    private void piecesGUI() {
        appGui.setCursor(new Cursor(3));
        if (piecesCtrl == null) {
            piecesCtrl = new PiecesController(param);
            appGui.getDesktop().add(piecesCtrl.getGUI(), JLayeredPane.DEFAULT_LAYER);
            piecesCtrl.maximize();
        } else if (piecesCtrl.init()) {
            appGui.getDesktop().add(piecesCtrl.getGUI(), JLayeredPane.DEFAULT_LAYER);
            piecesCtrl.maximize();
        }
        appGui.setCursor(new Cursor(0));
    }

    private void stockGUI() {
        appGui.setCursor(new Cursor(3));
        if (stockCtrl == null) {
            stockCtrl = new StockController(param);
            appGui.getDesktop().add(stockCtrl.getGUI(), JLayeredPane.DEFAULT_LAYER);
            stockCtrl.maximize();
        } else if (stockCtrl.init()) {
            appGui.getDesktop().add(stockCtrl.getGUI(), JLayeredPane.DEFAULT_LAYER);
            stockCtrl.maximize();
        }
        appGui.setCursor(new Cursor(0));
    }

    private void searchGUI() {
        appGui.setCursor(new Cursor(3));
        if (searchCtrl == null || searchCtrl.isClosed()) {
            searchCtrl = null;
            searchCtrl = new SearchController(param);
            searchCtrl.setIconifiable(true);
            searchCtrl.setResizable(false);
            searchCtrl.setMaximizable(false);
            searchCtrl.setClosable(true);
            screen = appGui.getDesktop().getSize();
            searchCtrl.setBounds(0, 0, screen.width, screen.height);
            appGui.getDesktop().add(searchCtrl, JLayeredPane.DEFAULT_LAYER);
        }
        if (searchCtrl.isIcon()) {
            try {
                searchCtrl.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!searchCtrl.isVisible()) {
            searchCtrl.setVisible(true);
        }
        searchCtrl.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void agencyGUI() {
        appGui.setCursor(new Cursor(3));
        if (agencyCtrl == null || agencyCtrl.isClosed()) {
            agencyCtrl = null;
            agencyCtrl = new AgencyController(param);
            agencyCtrl.setIconifiable(true);
            agencyCtrl.setResizable(false);
            agencyCtrl.setMaximizable(false);
            agencyCtrl.setClosable(true);
            screen = appGui.getDesktop().getSize();
            agencyCtrl.setBounds(screen.width / 2 - 165, screen.height / 2 - 210, 330, 420);
            appGui.getDesktop().add(agencyCtrl, JLayeredPane.DEFAULT_LAYER);
        }
        if (agencyCtrl.isIcon()) {
            try {
                agencyCtrl.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!agencyCtrl.isVisible()) {
            agencyCtrl.setVisible(true);
        }
        agencyCtrl.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void dbGUI() {
        appGui.setCursor(new Cursor(3));
        if (dbCtrl == null || dbCtrl.isClosed()) {
            dbCtrl = null;
            dbCtrl = new DbController(param);
            dbCtrl.setIconifiable(true);
            dbCtrl.setResizable(false);
            dbCtrl.setMaximizable(false);
            dbCtrl.setClosable(true);
            screen = appGui.getDesktop().getSize();
            dbCtrl.setBounds(screen.width / 2 - 165, screen.height / 2 - 210, 330, 450);
            appGui.getDesktop().add(dbCtrl, JLayeredPane.DEFAULT_LAYER);
        }
        if (dbCtrl.isIcon()) {
            try {
                dbCtrl.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!dbCtrl.isVisible()) {
            dbCtrl.setVisible(true);
        }
        dbCtrl.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void itemGUI() {
        appGui.setCursor(new Cursor(3));
        if (itemCtrl == null || itemCtrl.isClosed()) {
            itemCtrl = null;
            itemCtrl = new ItemController(param);
            itemCtrl.setIconifiable(true);
            itemCtrl.setResizable(false);
            itemCtrl.setMaximizable(false);
            itemCtrl.setClosable(true);
            screen = appGui.getDesktop().getSize();
            itemCtrl.setBounds(screen.width / 2 - 170, screen.height / 2 - 275, 340, 550);
            appGui.getDesktop().add(itemCtrl, JLayeredPane.DEFAULT_LAYER);
        }
        if (itemCtrl.isIcon()) {
            try {
                itemCtrl.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!itemCtrl.isVisible()) {
            itemCtrl.setVisible(true);
        }
        itemCtrl.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void customerGUI() {
        appGui.setCursor(new Cursor(3));
        if (customerCtrl == null || customerCtrl.isClosed()) {
            customerCtrl = null;
            customerCtrl = new CustomerController(param);
            customerCtrl.setIconifiable(true);
            customerCtrl.setResizable(false);
            customerCtrl.setMaximizable(false);
            customerCtrl.setClosable(true);
            screen = appGui.getDesktop().getSize();
            customerCtrl.setBounds(screen.width / 2 - 170, screen.height / 2 - 241, 340, 482);
            appGui.getDesktop().add(customerCtrl, JLayeredPane.DEFAULT_LAYER);
        }
        if (customerCtrl.isIcon()) {
            try {
                customerCtrl.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!customerCtrl.isVisible()) {
            customerCtrl.setVisible(true);
        }
        customerCtrl.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void salesPersonGUI() {
        appGui.setCursor(new Cursor(3));
        if (salesPersonCtrl == null || salesPersonCtrl.isClosed()) {
            salesPersonCtrl = null;
            salesPersonCtrl = new SalesPersonController(param);
            salesPersonCtrl.setIconifiable(true);
            salesPersonCtrl.setResizable(false);
            salesPersonCtrl.setMaximizable(false);
            salesPersonCtrl.setClosable(true);
            screen = appGui.getDesktop().getSize();
            salesPersonCtrl.setBounds(screen.width / 2 - 165, screen.height / 2 - 255, 330, 510);
            appGui.getDesktop().add(salesPersonCtrl, JLayeredPane.DEFAULT_LAYER);
        }
        if (salesPersonCtrl.isIcon()) {
            try {
                salesPersonCtrl.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!salesPersonCtrl.isVisible()) {
            salesPersonCtrl.setVisible(true);
        }
        salesPersonCtrl.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void familyGUI() {
        appGui.setCursor(new Cursor(3));
        if (familyCtrl == null || familyCtrl.isClosed()) {
            familyCtrl = null;
            familyCtrl = new FamilyController(param);
            familyCtrl.setIconifiable(false);
            familyCtrl.setResizable(false);
            familyCtrl.setMaximizable(false);
            familyCtrl.setClosable(true);
            screen = appGui.getDesktop().getSize();
            familyCtrl.setBounds(screen.width / 2 - 300, screen.height / 2 - 200, 600, 400);
            appGui.getDesktop().add(familyCtrl, JLayeredPane.DEFAULT_LAYER);
        }
        if (familyCtrl.isIcon()) {
            try {
                familyCtrl.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!familyCtrl.isVisible()) {
            familyCtrl.setVisible(true);
        }
        familyCtrl.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void supplierGUI() {
        appGui.setCursor(new Cursor(3));
        if (supplierCtrl == null || supplierCtrl.isClosed()) {
            supplierCtrl = null;
            supplierCtrl = new SupplierController(param);
            supplierCtrl.setIconifiable(true);
            supplierCtrl.setResizable(false);
            supplierCtrl.setMaximizable(false);
            supplierCtrl.setClosable(true);
            screen = appGui.getDesktop().getSize();
            supplierCtrl.setBounds(screen.width / 2 - 170, screen.height / 2 - 241, 340, 482);
            appGui.getDesktop().add(supplierCtrl, JLayeredPane.DEFAULT_LAYER);
        }
        if (supplierCtrl.isIcon()) {
            try {
                supplierCtrl.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!supplierCtrl.isVisible()) {
            supplierCtrl.setVisible(true);
        }
        supplierCtrl.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void taxGUI() {
        appGui.setCursor(new Cursor(3));
        if (taxCtrl == null || taxCtrl.isClosed()) {
            taxCtrl = null;
            taxCtrl = new TaxController(param);
            taxCtrl.setIconifiable(false);
            taxCtrl.setResizable(false);
            taxCtrl.setMaximizable(false);
            taxCtrl.setClosable(true);
            screen = appGui.getDesktop().getSize();
            taxCtrl.setBounds(screen.width / 2 - 150, screen.height / 2 - 100, 300, 130);
            appGui.getDesktop().add(taxCtrl, JLayeredPane.DEFAULT_LAYER);
        }
        if (taxCtrl.isIcon()) {
            try {
                taxCtrl.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!taxCtrl.isVisible()) {
            taxCtrl.setVisible(true);
        }
        taxCtrl.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void userGUI() {
        appGui.setCursor(new Cursor(3));
        if (userCtrl == null || userCtrl.isClosed()) {
            userCtrl = null;
            userCtrl = new UserController(param);
            userCtrl.setIconifiable(true);
            userCtrl.setResizable(false);
            userCtrl.setMaximizable(false);
            userCtrl.setClosable(true);
            screen = appGui.getDesktop().getSize();
            userCtrl.setBounds(screen.width / 2 - 165, screen.height / 2 - 204, 330, 408);
            appGui.getDesktop().add(userCtrl, JLayeredPane.DEFAULT_LAYER);
        }
        if (userCtrl.isIcon()) {
            try {
                userCtrl.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!userCtrl.isVisible()) {
            userCtrl.setVisible(true);
        }
        userCtrl.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void buyListGUI() {
        appGui.setCursor(new Cursor(3));
        if (buyList == null || buyList.isClosed()) {
            buyList = null;
            buyList = new OLD_BuyListGUI();
            buyList.setIconifiable(true);
            buyList.setResizable(false);
            buyList.setMaximizable(false);
            buyList.setClosable(true);
            buyList.setParameter(param);
            screen = appGui.getDesktop().getSize();
            buyList.setBounds(0, 0, screen.width, screen.height);
            appGui.getDesktop().add(buyList, JLayeredPane.DEFAULT_LAYER);
        }
        if (buyList.isIcon()) {
            try {
                buyList.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!buyList.isVisible()) {
            buyList.setVisible(true);
        }
        buyList.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void itemListGUI() {
        appGui.setCursor(new Cursor(3));
        if (itemList == null || itemList.isClosed()) {
            itemList = null;
            itemList = new OLD_ItemListGUI();
            itemList.setIconifiable(true);
            itemList.setResizable(false);
            itemList.setMaximizable(false);
            itemList.setClosable(true);
            itemList.setParameter(param);
            screen = appGui.getDesktop().getSize();
            itemList.setBounds(0, 0, screen.width, screen.height);
            appGui.getDesktop().add(itemList, JLayeredPane.DEFAULT_LAYER);
        }
        if (itemList.isIcon()) {
            try {
                itemList.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!itemList.isVisible()) {
            itemList.setVisible(true);
        }
        itemList.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void customerListGUI() {
        appGui.setCursor(new Cursor(3));
        if (customerList == null || customerList.isClosed()) {
            customerList = null;
            customerList = new OLD_CustomerListGUI();
            customerList.setIconifiable(true);
            customerList.setResizable(false);
            customerList.setMaximizable(false);
            customerList.setClosable(true);
            customerList.setParameter(param);
            screen = appGui.getDesktop().getSize();
            customerList.setBounds(0, 0, screen.width, screen.height);
            appGui.getDesktop().add(customerList, JLayeredPane.DEFAULT_LAYER);
        }
        if (customerList.isIcon()) {
            try {
                customerList.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!customerList.isVisible()) {
            customerList.setVisible(true);
        }
        customerList.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void supplierListGUI() {
        appGui.setCursor(new Cursor(3));
        if (supplierList == null || supplierList.isClosed()) {
            supplierList = null;
            supplierList = new OLD_SupplierListGUI();
            supplierList.setIconifiable(true);
            supplierList.setResizable(false);
            supplierList.setMaximizable(false);
            supplierList.setClosable(true);
            supplierList.setParameter(param);
            screen = appGui.getDesktop().getSize();
            supplierList.setBounds(0, 0, screen.width, screen.height);
            appGui.getDesktop().add(supplierList, JLayeredPane.DEFAULT_LAYER);
        }
        if (supplierList.isIcon()) {
            try {
                supplierList.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!supplierList.isVisible()) {
            supplierList.setVisible(true);
        }
        supplierList.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void saleListGUI() {
        appGui.setCursor(new Cursor(3));
        if (saleList == null || saleList.isClosed()) {
            saleList = null;
            saleList = new OLD_SaleListGUI();
            saleList.setIconifiable(true);
            saleList.setResizable(false);
            saleList.setMaximizable(false);
            saleList.setClosable(true);
            saleList.setParameter(param);
            screen = appGui.getDesktop().getSize();
            saleList.setBounds(0, 0, screen.width, screen.height);
            appGui.getDesktop().add(saleList, JLayeredPane.DEFAULT_LAYER);
        }
        if (saleList.isIcon()) {
            try {
                saleList.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!saleList.isVisible()) {
            saleList.setVisible(true);
        }
        saleList.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void exportGUI() {
        appGui.setCursor(new Cursor(3));
        if (exportCtrl == null || exportCtrl.isClosed()) {
            exportCtrl = null;
            exportCtrl = new ExportController(param);
            exportCtrl.setIconifiable(true);
            exportCtrl.setResizable(false);
            exportCtrl.setMaximizable(false);
            exportCtrl.setClosable(true);
            screen = appGui.getDesktop().getSize();
            exportCtrl.setBounds(screen.width / 2 - 165, screen.height / 2 - 97, 330, 194);
            appGui.getDesktop().add(exportCtrl, JLayeredPane.DEFAULT_LAYER);
        }
        if (exportCtrl.isIcon()) {
            try {
                exportCtrl.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!exportCtrl.isVisible()) {
            exportCtrl.setVisible(true);
        }
        exportCtrl.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void buildGUI() {
        appGui.setCursor(new Cursor(3));
        if (buildCtlr == null || buildCtlr.isClosed()) {
            buildCtlr = null;
            buildCtlr = new BuildController(param);
            buildCtlr.setIconifiable(true);
            buildCtlr.setResizable(false);
            buildCtlr.setMaximizable(false);
            buildCtlr.setClosable(true);
            screen = appGui.getDesktop().getSize();
            buildCtlr.setBounds(0, 0, screen.width, screen.height);
            appGui.getDesktop().add(buildCtlr, JLayeredPane.DEFAULT_LAYER);
        }
        if (buildCtlr.isIcon()) {
            try {
                buildCtlr.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!buildCtlr.isVisible()) {
            buildCtlr.setVisible(true);
        }
        buildCtlr.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void statisticsGUI() {
        appGui.setCursor(new Cursor(3));
        if (statisticsCtrl == null || statisticsCtrl.isClosed()) {
            statisticsCtrl = null;
            statisticsCtrl = new StatisticsController(param);
            statisticsCtrl.setIconifiable(true);
            statisticsCtrl.setResizable(false);
            statisticsCtrl.setMaximizable(false);
            statisticsCtrl.setClosable(true);
            screen = appGui.getDesktop().getSize();
            statisticsCtrl.setBounds(0, 0, screen.width, screen.height);
            appGui.getDesktop().add(statisticsCtrl, JLayeredPane.DEFAULT_LAYER);
        }
        if (statisticsCtrl.isIcon()) {
            try {
                statisticsCtrl.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!statisticsCtrl.isVisible()) {
            statisticsCtrl.setVisible(true);
        }
        statisticsCtrl.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void preferencesGUI() {
        appGui.setCursor(new Cursor(3));
        if (preferencesCtrl == null) {
            preferencesCtrl = new PreferencesController(param);
            appGui.getDesktop().add(preferencesCtrl.getPreferencesGUI(), JLayeredPane.DEFAULT_LAYER);
        } else {
            appGui.getDesktop().add(preferencesCtrl.getPreferencesGUI(), JLayeredPane.DEFAULT_LAYER);
        }
        try {
            preferencesCtrl.getPreferencesGUI().setSelected(true);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        appGui.setCursor(new Cursor(0));
    }

    private void aboutGUI() {
        appGui.setCursor(new Cursor(3));
        if (about == null || about.isClosed()) {
            about = null;
            about = new AboutGUI();
            about.setIconifiable(true);
            about.setResizable(false);
            about.setMaximizable(false);
            about.setClosable(true);
            about.setLiscenceNumberL(Messages.getString("MainController.serial_number") + prop.getSerial());
            about.getOkB().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    about.setVisible(false);
                    about = null;
                }
            });
            screen = appGui.getDesktop().getSize();
            about.setBounds(screen.width / 2 - 302, screen.height / 2 - 250, 604, 500);
            appGui.getDesktop().add(about, JLayeredPane.DEFAULT_LAYER);
        }
        if (about.isIcon()) {
            try {
                about.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!about.isVisible()) {
            about.setVisible(true);
        }
        about.toFront();
        appGui.setCursor(new Cursor(0));
    }

    public void help() {
        BrowserControl.displayURL("code.google.com/p/jgestcom/w/list");
    }

    private void changeLookAndFeel(int n, boolean modify) {
        try {
            lnf = new AlloyLookAndFeel();
            MetalLookAndFeel wlaf = null;
            switch (n) {
                case 0: // '\0'
                    lnf.setTheme(new DefaultAlloyTheme(), true);
                    break;

                case 1: // '\001'
                    lnf.setTheme(new BedouinTheme(), true);
                    break;

                case 2: // '\002'
                    lnf.setTheme(new AcidTheme(), true);
                    break;

                case 3: // '\003'
                    lnf.setTheme(new GlassTheme(), true);
                    break;

                case 4: // '\004'
                    wlaf = new MetalLookAndFeel();
                    break;
            }
            if (n == 4) {
                UIManager.setLookAndFeel(wlaf);
                if (appGui != null) {
                    SwingUtilities.updateComponentTreeUI(appGui);
                }
            } else {
                UIManager.setLookAndFeel(lnf);
                AlloyLookAndFeel.updateAllUIs();
            }
            if (modify) {
                PreferencesCRUD pref = new PreferencesCRUD();
                pref.setLookAndFeel(Integer.toString(n));
                pref.update();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void printingModel() {
        appGui.setCursor(new Cursor(3));
        if (printOptionsCtrl == null || printOptionsCtrl.isClosed()) {
            printOptionsCtrl = null;
            printOptionsCtrl = new PrintOptionsController(param);
            printOptionsCtrl.setIconifiable(false);
            printOptionsCtrl.setResizable(false);
            printOptionsCtrl.setMaximizable(false);
            printOptionsCtrl.setClosable(true);
            screen = appGui.getDesktop().getSize();
            printOptionsCtrl.setBounds(screen.width / 2 - 450, screen.height / 2 - 320, 900, 640);
            appGui.getDesktop().add(printOptionsCtrl, JLayeredPane.DEFAULT_LAYER);
        }
        if (printOptionsCtrl.isIcon()) {
            try {
                printOptionsCtrl.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        if (!printOptionsCtrl.isVisible()) {
            printOptionsCtrl.setVisible(true);
        }
        printOptionsCtrl.toFront();
        appGui.setCursor(new Cursor(0));
    }

    private void exit() {
        int reponse = JOptionPane.showConfirmDialog(null, Messages.getString("MainController.exit_msg"), Messages.getString("MainController.confirmation"), 1, 2, iconIF);
        switch (reponse) {
            case 0: // '\0'
                close();
            // fall through

            case -1:
            case 1: // '\001'
            case 2: // '\002'
            default:
                return;
        }
    }

    private void close() {
        if (connectDB.isConnected()) {
            connectDB.close();
        }
        System.exit(0);
    }

    public static void main(String args[]) {
        new MainController();
    }
    private AppMainGUI appGui;
    private ConnectionGUI connection;
    private PiecesController piecesCtrl;
    private StockController stockCtrl;
    private AgencyController agencyCtrl;
    private ItemController itemCtrl;
    private CustomerController customerCtrl;
    private SalesPersonController salesPersonCtrl;
    private FamilyController familyCtrl;
    private SupplierController supplierCtrl;
    private TaxController taxCtrl;
    private UserController userCtrl;
    private OLD_BuyListGUI buyList;
    private OLD_ItemListGUI itemList;
    private OLD_CustomerListGUI customerList;
    private OLD_SupplierListGUI supplierList;
    private OLD_SaleListGUI saleList;
    private ExportController exportCtrl;
    private SearchController searchCtrl;
    private PreferencesController preferencesCtrl;
    private PrintOptionsController printOptionsCtrl;
    private AboutGUI about;
    private AlloyLookAndFeel lnf;
    private PropertyCRUD prop;
    private PropertyGUI propGui;
    private StatisticsController statisticsCtrl;
    private BuildController buildCtlr;
    private DbController dbCtrl;
    private static Dimension screen;
    private ImageIcon iconIF;
    private Parameter param;
    private ConnectDB connectDB;
    private ObjectCRUD crud;
    private boolean msg;
    private String msgTxt;
    private MsgController msgCtrl;
}
